﻿using System.Collections.Generic;
using System.Web.Mvc;
using CommerceOne.Common;
using CommerceOne.Domains.Commons;
using CommerceOne.Domains.Entities;
using CommerceOne.Domains.Repositories;
using CommerceOne.Domains.Services;
using CommerceOne.Domains.SqlRepositories;
using CommerceOne.Models;
using CommerceOne.Widgets.FilterNavigation;
using CommerceOne.Widgets.PagingAndSortingNav;

namespace CommerceOne.Controllers
{
    public class DepartmentController : ControllerBase
    {
        private readonly IProductRepository _productRepository = new SqlProductRepository();
        private readonly ICategoryRepository _departmentRepository = new SqlCategoryRepository();

        /// <summary>
        /// Indexes the specified deparment id.
        /// </summary>
        /// <param name="objectId">The deparment id.</param>
        /// <param name="page">The page number.</param>
        /// <returns></returns>
        public ActionResult Index(int objectId, int page)
        {
            DepartmentViewModel viewModel = new DepartmentViewModel();
            this.CommonInitialize(viewModel);

            string filterString = Request.QueryString["filter"];

            NavigationFilterController filterController = new NavigationFilterController();
            filterController.FilterStr = filterString;
            List<IFilterParam> filterParams = filterController.MakeSearchParam();

            IFilterParam department = new FilterParam<int>("ProductSearchView", "CategoryId", FilterType.Equal, objectId);
            filterParams.Add(department);

            ISearchService searchService = new SearchService();
            SearchProductResult searchResult = searchService.SearchProduct(string.Empty, filterParams);

            filterController.SearchResult = searchResult;
            viewModel.NavigationViewModel = filterController.GetViewModel(PageType.Department, objectId);

            int size = 3;
            int totalRecord = filterController.SearchResult.Total;
            

            PagingSortingNavController pagingSortingController = new PagingSortingNavController();
            viewModel.PagingSortingViewModel = pagingSortingController.GetViewModel(totalRecord, size, page, "/" + objectId + "/Department/{0}");
            viewModel.DepartmentProductList = filterController.SearchResult.GetProduct(page, size);

            viewModel.CurrentDepartment = this._departmentRepository.GetById(objectId);

            return View(viewModel);
        }

        /// <summary>
        /// Alls the department.
        /// </summary>
        /// <returns></returns>
        public ActionResult AllDepartment()
        {
            List<Category> allCategories = this._departmentRepository.GetByParentId(null);
            AllDepartmentViewModel viewModel = new AllDepartmentViewModel();
            viewModel.AllCategory = allCategories;
            this.CommonInitialize(viewModel);
            return View(viewModel);
        }

    }
}
